<template>
	<view class="container">
		<!-- 轮播图 -->
		<view :style="' margin-top:' + (banner.style && banner.style.margin_top) + 'rpx;'"
			v-cloak>
			<view class="swiper-wrap" :style="{height:banner.style.height + 'rpx'}">
				<swiper autoplay @change="change">
					<swiper-item v-for="(item, index) in banner.list" :key="index">
						<image mode="aspectFill" :src="item.image + '?x-oss-process=image/resize,w_1200'"
							:style="{'border-radius':banner.style.radius + 'rpx'}" class="slide-image" :data-item="item"
							@tap="handleClick" />
					</swiper-item>
				</swiper>
				<view class="dots">
					<view class="dot-item" :class="{active:current==index}" v-for="(item,index) in banner.list"
						:key="index">
					</view>
				</view>
			</view>
		</view>
		<view class="func">
			<view class="item" @click="gotoFuncEvent(0)">
				<image class="cover" src="../../static/dayTask/share.png"></image>
				<view class="title">优惠券</view>
			</view>
			<view class="item" @click="gotoFuncEvent(1)">
				<image class="cover" src="../../static/dayTask/share.png"></image>
				<view class="title">购物车</view>
			</view>
			<view class="item" @click="gotoFuncEvent(2)">
				<image class="cover" src="../../static/dayTask/share.png"></image>
				<view class="title">收货地址</view>
			</view>
			<view class="item" @click="gotoFuncEvent(3)">
				<image class="cover" src="../../static/dayTask/share.png"></image>
				<view class="title">用户协议</view>
			</view>
		</view>
		<!-- tab切换区 -->
		<view class="category">
			<view class="def_cls" :class="[selected==2?'ac':'']" @tap="menu(2)">随机区 <text class="bottom-line"></text> </view>
			<view class="def_cls" :class="[selected==1?'ac':'']" @tap="menu(1)">擂台区 <text class="bottom-line"></text></view>
			<view class="def_cls" :class="[selected==3?'ac':'']" @tap="menu(3)">抽赏区 <text class="bottom-line"></text></view>
			<view class="def_cls" :class="[selected==4?'ac':'']" @tap="menu(4)">积分区 <text class="bottom-line"></text></view>
		</view>
		

		<view class="product_list" v-if="!empty">
			<view class="item" v-for="item in listInfo" @tap="info(item)">
				<image class="cover" :src="item.thumb" mode="widthFix"></image>
				<view class="info">
					<view class="title">
						{{item.title}}
					</view>
					<view class="prize" v-if="item.money_price > 0">
					
						<view class="left">
							<span style="font-size: 22rpx;">￥</span>
							<span style="font-weight: bold;">{{formatPrice(item.money_price)}}</span>
							<span style="font-size: 22rpx;color: #A6A6A6;">/张</span>
						</view>
						<view class="right">
							<span v-if="item.play_type ==5 || item.play_type ==4">1/{{item.now_id}}箱</span>
							<span v-else>{{item.rand}}/{{item.cnt}}箱</span>
						</view>
					</view>
					<view class="prize" v-else>
						<view class="left">
							<span style="font-size: 22rpx;transform: scale(0.5);margin-left: 10rpx;">积分</span>
							<span style="font-weight: bold;">{{item.score_price}}</span>
							<span style="font-size: 22rpx;color: #A6A6A6;">/张</span>
						</view>
						<view class="right">
							<span v-if="item.play_type ==5 || item.play_type ==4">1/{{item.now_id}}箱</span>
							<span v-else>{{item.rand}}/{{item.cnt}}箱</span>
						</view>
					</view>
				</view>
			</view>
			<view class="item"></view>
		</view>

		<view class="empty" v-else>
			<view>暂无活动</view>
			<image src="../../static/activity/qin.png" mode="widthFix"></image>
		</view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				zsac: 'ac',
				sqac: '',
				listInfo: '',
				stock_no: 1,
				empty: true,
				playType: '/leitais_group?play_type=3',
				infoType: 'leiTai',
				scrollTop: 0,
				refreshCounter1: 0,
				getNextPageCounter: 0,
				selected: 2,
				banner: [],
				current: 0
			}
		},

		onLoad() { 
			// console.log(1234)
			this.$http('/setting/base').then(res => {
				// console.log(res.data.setting.shop_home.modules.filter((e)=>e.type=="swiper"))
				res.data.setting.shop_home.modules.forEach(item => {
					if (item.type == 'swiper') {
						this.banner = item
						console.info(this.banner)
					}
				})

			})

			this.getData()
		},

		// onPullDownRefresh() {
		// 	this.$showPullRefresh().then(res => {
		// 		this.refreshCounter1++
		// 	})
		// },

		// onReachBottom() {
		// 	this.getNextPageCounter++
		// 	// console.log('reach bottom')
		// },
		// onPageScroll(e) {
		// 	this.scrollTop = e.scrollTop
		// },


		methods: {
			gotoFuncEvent(type){
				if(type == 0){
					uni.navigateTo({
						url:'/pages/myCoupons/index'
					})
				}else if(type ==1){
					uni.navigateTo({
						url:'/pages/cart/index'
					})
				}else if(type ==2){
					uni.navigateTo({
						url:'/pages/myAddress/index'
					})
				}else if(type ==3){
					uni.navigateTo({
						url:'/pages/rule/index'
					})
				}
			},
			st_load(){
				console.info(111)
			},
			st_dis(){
				console.info(11)
			},
			randomNumber(num) {
				console.log(31241);
				return Math.floor(Math.random() * num + 1);
			},
			change(e) {
				this.current = e.detail.current
			},
			handleClick(e) {
				let item = e.currentTarget.dataset.item
				let link = item.link || {}
				this.toLink(link)
			},
			GetRequest(url) {
				//获取url中"?"符后的字串  
				var theRequest = new Object();
				if (url.indexOf("?") != -1) {
					url = "?" + url.split("?")[1]; //模拟获取参数后面的
					var str = url.substr(1);
					let strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);

					}
				}
				return theRequest;
			},
			menu(e) {
				this.selected = e
				if (e == 1) {
					this.playType = '/leitais_group';
					this.infoType = 'leiTai';
				} else if (e == 2) {
					this.playType = '/leitais_group?play_type=3';
					this.infoType = 'leiTai';
				} else if (e == 3) {
					this.playType = '/choushangs_group?play_type=1';
					this.infoType = 'chouShang';
				} else {
					this.playType = '/choushangs_group?play_type=2';
					this.infoType = 'chouShang';
				}

				uni.showLoading({
					'mask': true,
					'title': '加载中...'
				})

				this.getData();
			},

			getData() {
				var url = this.playType;
				this.$http(url, 'GET', {
					stock_no: this.stock_no
				}).then(res => {
					console.log(res);
					// 2.3.27 新增替换需求
					this.listInfo = res.data.map((e) => {
						let app_title = e.title_app;
						e.title = app_title ? app_title : e.title;
						return e
					});
					if (res.data.length > 0) {
						this.empty = false;
						this.listInfo.forEach(item => {
							item.rand = this.randomNumber(item.cnt);
						})
					} else {
						this.empty = true;
					}

					uni.hideLoading()
				})
			},

			check(e) {
				if (e == 1) {
					this.zsac = 'ac';
					this.sqac = '';
					this.stock_no = 1;
					this.getData()
				} else {
					this.zsac = '';
					this.sqac = 'ac';
					this.stock_no = 0;
					this.getData() 
				}
			},

			info(e) {
				var url = "/packageA/pages/" + this.infoType + "/index?title=";
				console.info(url)
				uni.navigateTo({
					url: url + encodeURIComponent(e.title) + "&imgUrl=" + e.thumb + '&index='+e.id
				})
			},
			formatPrice(price) {
				return (price / 100).toFixed(2)
			},
		},


	}
</script>

<style lang="scss" scoped>
	.container {
		background: #fff;
		padding: 0 30rpx;
	}

	.swiper-wrap {
		width: 100%;
		swiper {
			height: 100%;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.dots {
		position: relative;
		left: 0;
		bottom: 30rpx;
		width: 100%;
		height: 8rpx;
		display: flex;
		justify-content: center;

		.dot-item {
			margin: 0 6rpx;
			width: 10rpx;
			height: 10rpx;
			border-radius: 50%;
			background-color: rgba(255, 255, 255, 0.6);
			transition: all 0.4s;


		}

		.active {
			border-radius: 6rpx;
			width: 26rpx;
			background-color: #FD9D11;
		}
	}
	
	.func{
		margin-top: 30rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 0rpx 40rpx;
		.item{
			display: flex;
			flex-direction: column;
			align-items: center;
			.cover{
				width: 120rpx;
				height: 120rpx;
			}
			.title{
				font-size: 28rpx;
				font-weight: bold;
				margin-top: 10rpx;
			}
		}
	}

	.category {
		font-size: 28rpx;
		color: #AEAEAE;

		.def_cls {
			display: inline-block;
			text-align: left;
			width: 117rpx;
			height: 88rpx;
			line-height: 88rpx;
			position: relative;
		}

		.sq {
			display: inline-block;
			text-align: left;
			width: 117rpx;
			height: 88rpx;
			line-height: 88rpx;
			position: relative;
		}

		.ac {
			color: #262626;

			// border-bottom: 6rpx solid #000;
			.bottom-line {
				display: block;
			}
		}

		.bottom-line {
			position: absolute;
			width: 80rpx;
			height: 4rpx;
			background: #4A90E2;
			border-radius: 2rpx 2rpx 2rpx 2rpx;
			left: 0;
			bottom: 10rpx;
			display: none;
		}
	}

	.product_list {
		margin-top: 24rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		flex-direction: column;
	}

	.item {
		// width: 308rpx;
		margin-bottom: 24rpx;
		display: flex;
		flex-direction: row;
		
		.cover {
			width: 200rpx;
			height: 180rpx;
			border-radius: 18rpx;
		}

		.info{
			margin-left: 30rpx;
			display: flex;
			flex-direction: column;
			background-color: white;
			.title {
				font-size: 30rpx;
				font-family: PingFang;
				font-weight: bold;
				color: #484747;
				margin-top: 16rpx;
			}
			
			.prize {
				margin-top: 16rpx;
				display: flex;
				flex-direction: column;
				font-size: 26rpx;
				
				.left{
					
				}
				
				.right{
					margin-top: 26rpx;
				}
			}
		}
		


	}

	.empty {
		width: 100%;
		margin-top: 10vh;
		text-align: center;

		color: #A6A6A6;
		font-size: 25rpx;

		image {
			width: 20%;
		}
	}
</style>